home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sndhrdw / leland.c < prev    next >
C/C++ Source or Header  |  2000-05-23  |  62KB  |  2,269 lines

  1. /***************************************************************************
  2.  
  3.     Cinemat/Leland driver
  4.  
  5.     Leland sound hardware
  6.     driver by Aaron Giles and Paul Leaman
  7.  
  8.     -------------------------------------------------------------------
  9.  
  10.     1st generation sound hardware was controlled by the master Z80.
  11.     It drove an AY-8910/AY-8912 pair for music. It also had two DACs
  12.     that were driven by the video refresh. At the end of each scanline
  13.     there are 8-bit DAC samples that can be enabled via the output
  14.     ports on the AY-8910. The DACs run at a fixed frequency of 15.3kHz,
  15.     since they are clocked once each scanline.
  16.  
  17.     -------------------------------------------------------------------
  18.  
  19.     2nd generation sound hardware was used in Redline Racer. It
  20.     consisted of an 80186 microcontroller driving 8 8-bit DACs. The
  21.     frequency of the DACs were controlled by one of 3 Intel 8254
  22.     programmable interval timers (PITs):
  23.  
  24.         DAC number    Clock source
  25.         ----------    -----------------
  26.             0        8254 PIT 1 output 0
  27.             1        8254 PIT 1 output 1
  28.             2        8254 PIT 1 output 2
  29.             3        8254 PIT 2 output 0
  30.             4        8254 PIT 2 output 1
  31.             5-7        8254 PIT 3 output 0
  32.  
  33.     The clock outputs for each DAC can be read, and are polled to
  34.     determine when data should be updated on the chips. The 80186's
  35.     two DMA channels are generally used to drive the first two DACs,
  36.     with the remaining 6 DACs being fed manually via polling.
  37.  
  38.     -------------------------------------------------------------------
  39.  
  40.     3rd generation sound hardware appeared in the football games
  41.     (Quarterback, AAFB) and the later games up through Pigout. This
  42.     variant is closely based on the Redline Racer sound system, but
  43.     they took out two of the DACs and replaced them with a higher
  44.     resolution (10-bit) DAC. The driving clocks have been rearranged
  45.     a bit, and the number of PITs reduced from 3 to 2:
  46.  
  47.         DAC number    Clock source
  48.         ----------    -----------------
  49.             0        8254 PIT 1 output 0
  50.             1        8254 PIT 1 output 1
  51.             2        8254 PIT 1 output 2
  52.             3        8254 PIT 2 output 0
  53.             4        8254 PIT 2 output 1
  54.             5        8254 PIT 2 output 2
  55.             10-bit    80186 timer 0
  56.  
  57.     Like the 2nd generation board, the first two DACs are driven via
  58.     the DMA channels, and the remaining 5 DACs are polled.
  59.  
  60.     -------------------------------------------------------------------
  61.  
  62.     4th generation sound hardware showed up in Ataxx, Indy Heat, and
  63.     World Soccer Finals. For this variant, they removed one more PIT
  64.     and 3 of the 8-bit DACs, and added a YM2151 music chip and an
  65.     externally-fed 8-bit DAC.
  66.  
  67.         DAC number    Clock source
  68.         ----------    -----------------
  69.             0        8254 PIT 1 output 0
  70.             1        8254 PIT 1 output 1
  71.             2        8254 PIT 1 output 2
  72.             10-bit    80186 timer 0
  73.             ext        80186 timer 1
  74.  
  75.     The externally driven DACs have registers for a start/stop address
  76.     and triggers to control the clocking.
  77.  
  78. ***************************************************************************/
  79.  
  80. #include "driver.h"
  81. #include "cpu/i86/i186intf.h"
  82. #include "cpu/z80/z80.h"
  83.  
  84.  
  85. /*************************************
  86.  *
  87.  *    1st generation sound
  88.  *
  89.  *************************************/
  90.  
  91. #define DAC_BUFFER_SIZE        1024
  92. #define DAC_BUFFER_MASK        (DAC_BUFFER_SIZE - 1)
  93.  
  94. static UINT8 *dac_buffer[2];
  95. static UINT32 dac_bufin[2];
  96. static UINT32 dac_bufout[2];
  97.  
  98. static int dac_stream;
  99.  
  100. static void leland_update(int param, INT16 *buffer, int length)
  101. {
  102.     int dacnum;
  103.  
  104.     /* reset the buffer */
  105.     memset(buffer, 0, length * sizeof(INT16));
  106.     for (dacnum = 0; dacnum < 2; dacnum++)
  107.     {
  108.         int bufout = dac_bufout[dacnum];
  109.         int count = (dac_bufin[dacnum] - bufout) & DAC_BUFFER_MASK;
  110.  
  111.         if (count > 300)
  112.         {
  113.             UINT8 *base = dac_buffer[dacnum];
  114.             int i;
  115.  
  116.             for (i = 0; i < length && count > 0; i++, count--)
  117.             {
  118.                 buffer[i] += ((INT16)base[bufout] - 0x80) * 0x40;
  119.                 bufout = (bufout + 1) & DAC_BUFFER_MASK;
  120.             }
  121.             dac_bufout[dacnum] = bufout;
  122.         }
  123.     }
  124. }
  125.  
  126.  
  127. int leland_sh_start(const struct MachineSound *msound)
  128. {
  129.     /* reset globals */
  130.     dac_buffer[0] = dac_buffer[1] = NULL;
  131.     dac_bufin[0]  = dac_bufin[1]  = 0;
  132.     dac_bufout[0] = dac_bufout[1] = 0;
  133.  
  134.     /* skip if no sound */
  135.     if (Machine->sample_rate == 0)
  136.         return 0;
  137.  
  138.     /* allocate the stream */
  139.     dac_stream = stream_init("Onboard DACs", 50, 256*60, 0, leland_update);
  140.  
  141.     /* allocate memory */
  142.     dac_buffer[0] = malloc(DAC_BUFFER_SIZE);
  143.     dac_buffer[1] = malloc(DAC_BUFFER_SIZE);
  144.     if (!dac_buffer[0] || !dac_buffer[1])
  145.     {
  146.         if (dac_buffer[0]) free(dac_buffer[0]);
  147.         if (dac_buffer[1]) free(dac_buffer[1]);
  148.         dac_buffer[0] = dac_buffer[1] = NULL;
  149.         return 1;
  150.     }
  151.  
  152.     return 0;
  153. }
  154.  
  155.  
  156. void leland_sh_stop(void)
  157. {
  158.     if (dac_buffer[0])
  159.         free(dac_buffer[0]);
  160.     if (dac_buffer[1])
  161.         free(dac_buffer[1]);
  162.     dac_buffer[0] = dac_buffer[1] = NULL;
  163. }
  164.  
  165.  
  166. void leland_dac_update(int dacnum, UINT8 *base)
  167. {
  168.     UINT8 *buffer = dac_buffer[dacnum];
  169.     int bufin = dac_bufin[dacnum];
  170.     int row;
  171.  
  172.     /* skip if nothing */
  173.     if (!buffer)
  174.         return;
  175.  
  176.     /* copy data from VRAM */
  177.     for (row = 0; row < 256; row++)
  178.     {
  179.         buffer[bufin] = base[row * 0x80];
  180.         bufin = (bufin + 1) & DAC_BUFFER_MASK;
  181.     }
  182.  
  183.     /* update the buffer */
  184.     dac_bufin[dacnum] = bufin;
  185. }
  186.  
  187.  
  188.  
  189. /*************************************
  190.  *
  191.  *    2nd-4th generation sound
  192.  *
  193.  *************************************/
  194.  
  195. #define LOG_INTERRUPTS        0
  196. #define LOG_DMA                0
  197. #define LOG_SHORTAGES        0
  198. #define LOG_TIMER            0
  199. #define LOG_COMM            0
  200. #define LOG_PORTS            0
  201. #define LOG_DAC                0
  202. #define LOG_EXTERN            0
  203. #define LOG_PIT                0
  204. #define LOG_OPTIMIZATION    0
  205.  
  206.  
  207. /* according to the Intel manual, external interrupts are not latched */
  208. /* however, I cannot get this system to work without latching them */
  209. #define LATCH_INTS    1
  210.  
  211. #define DAC_VOLUME_SCALE    4
  212. #define CPU_RESUME_TRIGGER    7123
  213.  
  214.  
  215. static int dma_stream;
  216. static int nondma_stream;
  217. static int extern_stream;
  218.  
  219. static UINT8 *ram_base;
  220. static UINT8 has_ym2151;
  221. static UINT8 is_redline;
  222.  
  223. static UINT8 last_control;
  224. static UINT8 clock_active;
  225. static UINT8 clock_tick;
  226.  
  227. static UINT8 sound_command[2];
  228. static UINT8 sound_response;
  229.  
  230. static UINT32 ext_start;
  231. static UINT32 ext_stop;
  232. static UINT8 ext_active;
  233. static UINT8 *ext_base;
  234.  
  235. static UINT8 *active_mask;
  236. static int total_reads;
  237.  
  238. struct mem_state
  239. {
  240.     UINT16    lower;
  241.     UINT16    upper;
  242.     UINT16    middle;
  243.     UINT16    middle_size;
  244.     UINT16    peripheral;
  245. };
  246.  
  247. struct timer_state
  248. {
  249.     UINT16    control;
  250.     UINT16    maxA;
  251.     UINT16    maxB;
  252.     UINT16    count;
  253.     void *    int_timer;
  254.     void *    time_timer;
  255.     double    last_time;
  256. };
  257.  
  258. struct dma_state
  259. {
  260.     UINT32    source;
  261.     UINT32    dest;
  262.     UINT16    count;
  263.     UINT16    control;
  264.     UINT8    finished;
  265.     void *    finish_timer;
  266. };
  267.  
  268. struct intr_state
  269. {
  270.     UINT8    pending;
  271.     UINT16    ack_mask;
  272.     UINT16    priority_mask;
  273.     UINT16    in_service;
  274.     UINT16    request;
  275.     UINT16    status;
  276.     UINT16    poll_status;
  277.     UINT16    timer;
  278.     UINT16    dma[2];
  279.     UINT16    ext[4];
  280. };
  281.  
  282. static struct i186_state
  283. {
  284.     struct timer_state    timer[3];
  285.     struct dma_state    dma[2];
  286.     struct intr_state    intr;
  287.     struct mem_state    mem;
  288. } i186;
  289.  
  290.  
  291. #define DAC_BUFFER_SIZE            1024
  292. #define DAC_BUFFER_SIZE_MASK    (DAC_BUFFER_SIZE - 1)
  293. static struct dac_state
  294. {
  295.     INT16    value;
  296.     INT16    volume;
  297.     UINT32    frequency;
  298.     UINT32    step;
  299.     UINT32    fraction;
  300.  
  301.     INT16    buffer[DAC_BUFFER_SIZE];
  302.     UINT32    bufin;
  303.     UINT32    bufout;
  304.     UINT32    buftarget;
  305. } dac[8];
  306.  
  307. static struct counter_state
  308. {
  309.     void *timer;
  310.     INT32 count;
  311.     UINT8 mode;
  312.     UINT8 readbyte;
  313.     UINT8 writebyte;
  314. } counter[9];
  315.  
  316. static void set_dac_frequency(int which, int frequency);
  317.  
  318. static READ_HANDLER( peripheral_r );
  319. static WRITE_HANDLER( peripheral_w );
  320.  
  321.  
  322.  
  323. /*************************************
  324.  *
  325.  *    Manual DAC sound generation
  326.  *
  327.  *************************************/
  328.  
  329. static void leland_i186_dac_update(int param, INT16 *buffer, int length)
  330. {
  331.     int i, j, start, stop;
  332.  
  333.     if (LOG_SHORTAGES) logerror("----\n");
  334.  
  335.     /* reset the buffer */
  336.     memset(buffer, 0, length * sizeof(INT16));
  337.  
  338.     /* if we're redline racer, we have more DACs */
  339.     if (!is_redline)
  340.         start = 2, stop = 7;
  341.     else
  342.         start = 0, stop = 8;
  343.  
  344.     /* loop over manual DAC channels */
  345.     for (i = start; i < stop; i++)
  346.     {
  347.         struct dac_state *d = &dac[i];
  348.         int count = (d->bufin - d->bufout) & DAC_BUFFER_SIZE_MASK;
  349.  
  350.         /* if we have data, process it */
  351.         if (count > 0)
  352.         {
  353.             INT16 *base = d->buffer;
  354.             int source = d->bufout;
  355.             int frac = d->fraction;
  356.             int step = d->step;
  357.  
  358.             /* sample-rate convert to the output frequency */
  359.             for (j = 0; j < length && count > 0; j++)
  360.             {
  361.                 buffer[j] += base[source];
  362.                 frac += step;
  363.                 source += frac >> 24;
  364.                 count -= frac >> 24;
  365.                 frac &= 0xffffff;
  366.                 source &= DAC_BUFFER_SIZE_MASK;
  367.             }
  368.  
  369.             if (LOG_SHORTAGES && j < length)
  370.                 logerror("DAC #%d short by %d/%d samples\n", i, length - j, length);
  371.  
  372.             /* update the DAC state */
  373.             d->fraction = frac;
  374.             d->bufout = source;
  375.         }
  376.  
  377.         /* update the clock status */
  378.         if (count < d->buftarget)
  379.         {
  380.             if (LOG_OPTIMIZATION) logerror("  - trigger due to clock active in update\n");
  381.             cpu_trigger(CPU_RESUME_TRIGGER);
  382.             clock_active |= 1 << i;
  383.         }
  384.     }
  385. }
  386.  
  387.  
  388.  
  389. /*************************************
  390.  *
  391.  *    DMA-based DAC sound generation
  392.  *
  393.  *************************************/
  394.  
  395. static void leland_i186_dma_update(int param, INT16 *buffer, int length)
  396. {
  397.     int i, j;
  398.  
  399.     /* reset the buffer */
  400.     memset(buffer, 0, length * sizeof(INT16));
  401.  
  402.     /* loop over DMA buffers */
  403.     for (i = 0; i < 2; i++)
  404.     {
  405.         struct dma_state *d = &i186.dma[i];
  406.  
  407.         /* check for enabled DMA */
  408.         if (d->control & 0x0002)
  409.         {
  410.             /* make sure the parameters meet our expectations */
  411.             if ((d->control & 0xfe00) != 0x1600)
  412.             {
  413.                 logerror("Unexpected DMA control %02X\n", d->control);
  414.             }
  415.             else if (!is_redline && ((d->dest & 1) || (d->dest & 0x3f) > 0x0b))
  416.             {
  417.                 logerror("Unexpected DMA destination %02X\n", d->dest);
  418.             }
  419.             else if (is_redline && (d->dest & 0xf000) != 0x4000 && (d->dest & 0xf000) != 0x5000)
  420.             {
  421.                 logerror("Unexpected DMA destination %02X\n", d->dest);
  422.             }
  423.  
  424.             /* otherwise, we're ready for liftoff */
  425.             else
  426.             {
  427.                 UINT8 *base = memory_region(REGION_CPU3);
  428.                 int source = d->source;
  429.                 int count = d->count;
  430.                 int which, frac, step, volume;
  431.  
  432.                 /* adjust for redline racer */
  433.                 if (!is_redline)
  434.                     which = (d->dest & 0x3f) / 2;
  435.                 else
  436.                     which = (d->dest >> 9) & 7;
  437.  
  438.                 frac = dac[which].fraction;
  439.                 step = dac[which].step;
  440.                 volume = dac[which].volume;
  441.  
  442.                 /* sample-rate convert to the output frequency */
  443.                 for (j = 0; j < length && count > 0; j++)
  444.                 {
  445.                     buffer[j] += ((int)base[source] - 0x80) * volume;
  446.                     frac += step;
  447.                     source += frac >> 24;
  448.                     count -= frac >> 24;
  449.                     frac &= 0xffffff;
  450.                 }
  451.  
  452.                 /* update the DMA state */
  453.                 if (count > 0)
  454.                 {
  455.                     d->source = source;
  456.                     d->count = count;
  457.                 }
  458.                 else
  459.                 {
  460.                     /* let the timer callback actually mark the transfer finished */
  461.                     d->source = source + count - 1;
  462.                     d->count = 1;
  463.                     d->finished = 1;
  464.                 }
  465.  
  466.                 if (LOG_DMA) logerror("DMA Generated %d samples - new count = %04X, source = %04X\n", j, d->count, d->source);
  467.  
  468.                 /* update the DAC state */
  469.                 dac[which].fraction = frac;
  470.             }
  471.         }
  472.     }
  473. }
  474.  
  475.  
  476.  
  477. /*************************************
  478.  *
  479.  *    Externally-driven DAC sound generation
  480.  *
  481.  *************************************/
  482.  
  483. static void leland_i186_extern_update(int param, INT16 *buffer, int length)
  484. {
  485.     struct dac_state *d = &dac[7];
  486.     int count = ext_stop - ext_start;
  487.     int j;
  488.  
  489.     /* reset the buffer */
  490.     memset(buffer, 0, length * sizeof(INT16));
  491.  
  492.     /* if we have data, process it */
  493.     if (count > 0 && ext_active)
  494.     {
  495.         int source = ext_start;
  496.         int frac = d->fraction;
  497.         int step = d->step;
  498.  
  499.         /* sample-rate convert to the output frequency */
  500.         for (j = 0; j < length && count > 0; j++)
  501.         {
  502.             buffer[j] += ((INT16)ext_base[source] - 0x80) * d->volume;
  503.             frac += step;
  504.             source += frac >> 24;
  505.             count -= frac >> 24;
  506.             frac &= 0xffffff;
  507.         }
  508.  
  509.         /* update the DAC state */
  510.         d->fraction = frac;
  511.         ext_start = source;
  512.     }
  513. }
  514.  
  515.  
  516.  
  517. /*************************************
  518.  *
  519.  *    Sound initialization
  520.  *
  521.  *************************************/
  522.  
  523. int leland_i186_sh_start(const struct MachineSound *msound)
  524. {
  525.     int i;
  526.  
  527.     /* bail if nothing to play */
  528.     if (Machine->sample_rate == 0)
  529.         return 0;
  530.  
  531.     /* determine which sound hardware is installed */
  532.     has_ym2151 = 0;
  533.     for (i = 0; i < MAX_SOUND; i++)
  534.         if (Machine->drv->sound[i].sound_type == SOUND_YM2151)
  535.             has_ym2151 = 1;
  536.  
  537.     /* allocate separate streams for the DMA and non-DMA DACs */
  538.     dma_stream = stream_init("80186 DMA-driven DACs", 100, Machine->sample_rate, 0, leland_i186_dma_update);
  539.     nondma_stream = stream_init("80186 manually-driven DACs", 100, Machine->sample_rate, 0, leland_i186_dac_update);
  540.  
  541.     /* if we have a 2151, install an externally driven DAC stream */
  542.     if (has_ym2151)
  543.     {
  544.         ext_base = memory_region(REGION_SOUND1);
  545.         extern_stream = stream_init("80186 externally-driven DACs", 100, Machine->sample_rate, 0, leland_i186_extern_update);
  546.     }
  547.  
  548.     /* by default, we're not redline racer */
  549.     is_redline = 0;
  550.     return 0;
  551. }
  552.  
  553.  
  554. int redline_i186_sh_start(const struct MachineSound *msound)
  555. {
  556.     int result = leland_i186_sh_start(msound);
  557.     is_redline = 1;
  558.     return result;
  559. }
  560.  
  561.  
  562. static void leland_i186_reset(void)
  563. {
  564.     /* kill any live timers */
  565.     if (i186.timer[0].int_timer) timer_remove(i186.timer[0].int_timer);
  566.     if (i186.timer[1].int_timer) timer_remove(i186.timer[1].int_timer);
  567.     if (i186.timer[2].int_timer) timer_remove(i186.timer[2].int_timer);
  568.     if (i186.timer[0].time_timer) timer_remove(i186.timer[0].time_timer);
  569.     if (i186.timer[1].time_timer) timer_remove(i186.timer[1].time_timer);
  570.     if (i186.timer[2].time_timer) timer_remove(i186.timer[2].time_timer);
  571.     if (i186.dma[0].finish_timer) timer_remove(i186.dma[0].finish_timer);
  572.     if (i186.dma[1].finish_timer) timer_remove(i186.dma[1].finish_timer);
  573.  
  574.     /* reset the i186 state */
  575.     memset(&i186, 0, sizeof(i186));
  576.  
  577.     /* reset the interrupt state */
  578.     i186.intr.priority_mask    = 0x0007;
  579.     i186.intr.timer         = 0x000f;
  580.     i186.intr.dma[0]        = 0x000f;
  581.     i186.intr.dma[1]        = 0x000f;
  582.     i186.intr.ext[0]        = 0x000f;
  583.     i186.intr.ext[1]        = 0x000f;
  584.     i186.intr.ext[2]        = 0x000f;
  585.     i186.intr.ext[3]        = 0x000f;
  586.  
  587.     /* reset the DAC and counter states as well */
  588.     memset(&dac, 0, sizeof(dac));
  589.     memset(&counter, 0, sizeof(counter));
  590.  
  591.     /* send a trigger in case we're suspended */
  592.     if (LOG_OPTIMIZATION) logerror("  - trigger due to reset\n");
  593.     cpu_trigger(CPU_RESUME_TRIGGER);
  594.     total_reads = 0;
  595.  
  596.     /* reset the sound systems */
  597.     sound_reset();
  598. }
  599.  
  600.  
  601. void leland_i186_sound_init(void)
  602. {
  603.     /* RAM is multiply mapped in the first 128k of address space */
  604.     cpu_setbank(6, ram_base);
  605.     cpu_setbank(7, ram_base);
  606.  
  607.     /* reset the I86 registers */
  608.     memset(&i186, 0, sizeof(i186));
  609.     leland_i186_reset();
  610.  
  611.     /* reset our internal stuff */
  612.     last_control = 0xf8;
  613.     clock_active = 0;
  614.  
  615.     /* reset the external DAC */
  616.     ext_start = 0;
  617.     ext_stop = 0;
  618.     ext_active = 0;
  619. }
  620.  
  621.  
  622.  
  623. /*************************************
  624.  *
  625.  *    80186 interrupt controller
  626.  *
  627.  *************************************/
  628.  
  629. static int int_callback(int line)
  630. {
  631.     if (LOG_INTERRUPTS) logerror("(%f) **** Acknowledged interrupt vector %02X\n", timer_get_time(), i186.intr.poll_status & 0x1f);
  632.  
  633.     /* clear the interrupt */
  634.     i86_set_irq_line(0, CLEAR_LINE);
  635.     i186.intr.pending = 0;
  636.  
  637.     /* clear the request and set the in-service bit */
  638. #if LATCH_INTS
  639.     i186.intr.request &= ~i186.intr.ack_mask;
  640. #else
  641.     i186.intr.request &= ~(i186.intr.ack_mask & 0x0f);
  642. #endif
  643.     i186.intr.in_service |= i186.intr.ack_mask;
  644.     if (i186.intr.ack_mask == 0x0001)
  645.     {
  646.         switch (i186.intr.poll_status & 0x1f)
  647.         {
  648.             case 0x08:    i186.intr.status &= ~0x01;    break;
  649.             case 0x12:    i186.intr.status &= ~0x02;    break;
  650.             case 0x13:    i186.intr.status &= ~0x04;    break;
  651.         }
  652.     }
  653.     i186.intr.ack_mask = 0;
  654.  
  655.     /* a request no longer pending */
  656.     i186.intr.poll_status &= ~0x8000;
  657.  
  658.     /* return the vector */
  659.     return i186.intr.poll_status & 0x1f;
  660. }
  661.  
  662.  
  663. static void update_interrupt_state(void)
  664. {
  665.     int i, j, new_vector = 0;
  666.  
  667.     if (LOG_INTERRUPTS) logerror("update_interrupt_status: req=%02X stat=%02X serv=%02X\n", i186.intr.request, i186.intr.status, i186.intr.in_service);
  668.  
  669.     /* loop over priorities */
  670.     for (i = 0; i <= i186.intr.priority_mask; i++)
  671.     {
  672.         /* note: by checking 4 bits, we also verify that the mask is off */
  673.         if ((i186.intr.timer & 15) == i)
  674.         {
  675.             /* if we're already servicing something at this level, don't generate anything new */
  676.             if (i186.intr.in_service & 0x01)
  677.                 return;
  678.  
  679.             /* if there's something pending, generate an interrupt */
  680.             if (i186.intr.status & 0x07)
  681.             {
  682.                 if (i186.intr.status & 1)
  683.                     new_vector = 0x08;
  684.                 else if (i186.intr.status & 2)
  685.                     new_vector = 0x12;
  686.                 else if (i186.intr.status & 4)
  687.                     new_vector = 0x13;
  688.                 else
  689.                     usrintf_showmessage("Invalid timer interrupt!");
  690.  
  691.                 /* set the clear mask and generate the int */
  692.                 i186.intr.ack_mask = 0x0001;
  693.                 goto generate_int;
  694.             }
  695.         }
  696.  
  697.         /* check DMA interrupts */
  698.         for (j = 0; j < 2; j++)
  699.             if ((i186.intr.dma[j] & 15) == i)
  700.             {
  701.                 /* if we're already servicing something at this level, don't generate anything new */
  702.                 if (i186.intr.in_service & (0x04 << j))
  703.                     return;
  704.  
  705.                 /* if there's something pending, generate an interrupt */
  706.                 if (i186.intr.request & (0x04 << j))
  707.                 {
  708.                     new_vector = 0x0a + j;
  709.  
  710.                     /* set the clear mask and generate the int */
  711.                     i186.intr.ack_mask = 0x0004 << j;
  712.                     goto generate_int;
  713.                 }
  714.             }
  715.  
  716.         /* check external interrupts */
  717.         for (j = 0; j < 4; j++)
  718.             if ((i186.intr.ext[j] & 15) == i)
  719.             {
  720.                 /* if we're already servicing something at this level, don't generate anything new */
  721.                 if (i186.intr.in_service & (0x10 << j))
  722.                     return;
  723.  
  724.                 /* if there's something pending, generate an interrupt */
  725.                 if (i186.intr.request & (0x10 << j))
  726.                 {
  727.                     /* otherwise, generate an interrupt for this request */
  728.                     new_vector = 0x0c + j;
  729.  
  730.                     /* set the clear mask and generate the int */
  731.                     i186.intr.ack_mask = 0x0010 << j;
  732.                     goto generate_int;
  733.                 }
  734.             }
  735.     }
  736.     return;
  737.  
  738. generate_int:
  739.     /* generate the appropriate interrupt */
  740.     i186.intr.poll_status = 0x8000 | new_vector;
  741.     if (!i186.intr.pending)
  742.         cpu_set_irq_line(2, 0, ASSERT_LINE);
  743.     i186.intr.pending = 1;
  744.     cpu_trigger(CPU_RESUME_TRIGGER);
  745.     if (LOG_OPTIMIZATION) logerror("  - trigger due to interrupt pending\n");
  746.     if (LOG_INTERRUPTS) logerror("(%f) **** Requesting interrupt vector %02X\n", timer_get_time(), new_vector);
  747. }
  748.  
  749.  
  750. static void handle_eoi(int data)
  751. {
  752.     int i, j;
  753.  
  754.     /* specific case */
  755.     if (!(data & 0x8000))
  756.     {
  757.         /* turn off the appropriate in-service bit */
  758.         switch (data & 0x1f)
  759.         {
  760.             case 0x08:    i186.intr.in_service &= ~0x01;    break;
  761.             case 0x12:    i186.intr.in_service &= ~0x01;    break;
  762.             case 0x13:    i186.intr.in_service &= ~0x01;    break;
  763.             case 0x0a:    i186.intr.in_service &= ~0x04;    break;
  764.             case 0x0b:    i186.intr.in_service &= ~0x08;    break;
  765.             case 0x0c:    i186.intr.in_service &= ~0x10;    break;
  766.             case 0x0d:    i186.intr.in_service &= ~0x20;    break;
  767.             case 0x0e:    i186.intr.in_service &= ~0x40;    break;
  768.             case 0x0f:    i186.intr.in_service &= ~0x80;    break;
  769.             default:    logerror("%05X:ERROR - 80186 EOI with unknown vector %02X\n", cpu_get_pc(), data & 0x1f);
  770.         }
  771.         if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for vector %02X\n", timer_get_time(), data & 0x1f);
  772.     }
  773.  
  774.     /* non-specific case */
  775.     else
  776.     {
  777.         /* loop over priorities */
  778.         for (i = 0; i <= 7; i++)
  779.         {
  780.             /* check for in-service timers */
  781.             if ((i186.intr.timer & 7) == i && (i186.intr.in_service & 0x01))
  782.             {
  783.                 i186.intr.in_service &= ~0x01;
  784.                 if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for timer\n", timer_get_time());
  785.                 return;
  786.             }
  787.  
  788.             /* check for in-service DMA interrupts */
  789.             for (j = 0; j < 2; j++)
  790.                 if ((i186.intr.dma[j] & 7) == i && (i186.intr.in_service & (0x04 << j)))
  791.                 {
  792.                     i186.intr.in_service &= ~(0x04 << j);
  793.                     if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for DMA%d\n", timer_get_time(), j);
  794.                     return;
  795.                 }
  796.  
  797.             /* check external interrupts */
  798.             for (j = 0; j < 4; j++)
  799.                 if ((i186.intr.ext[j] & 7) == i && (i186.intr.in_service & (0x10 << j)))
  800.                 {
  801.                     i186.intr.in_service &= ~(0x10 << j);
  802.                     if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for INT%d\n", timer_get_time(), j);
  803.                     return;
  804.                 }
  805.         }
  806.     }
  807. }
  808.  
  809.  
  810.  
  811. /*************************************
  812.  *
  813.  *    80186 internal timers
  814.  *
  815.  *************************************/
  816.  
  817. static void internal_timer_int(int which)
  818. {
  819.     struct timer_state *t = &i186.timer[which];
  820.  
  821.     if (LOG_TIMER) logerror("Hit interrupt callback for timer %d\n", which);
  822.  
  823.     /* set the max count bit */
  824.     t->control |= 0x0020;
  825.  
  826.     /* request an interrupt */
  827.     if (t->control & 0x2000)
  828.     {
  829.         i186.intr.status |= 0x01 << which;
  830.         update_interrupt_state();
  831.         if (LOG_TIMER) logerror("  Generating timer interrupt\n");
  832.     }
  833.  
  834.     /* if we're continuous, reset */
  835.     if (t->control & 0x0001)
  836.     {
  837.         int count = t->maxA ? t->maxA : 0x10000;
  838.         t->int_timer = timer_set((double)count * TIME_IN_HZ(2000000), which, internal_timer_int);
  839.         if (LOG_TIMER) logerror("  Repriming interrupt\n");
  840.     }
  841.     else
  842.         t->int_timer = NULL;
  843. }
  844.  
  845.  
  846. static void internal_timer_sync(int which)
  847. {
  848.     struct timer_state *t = &i186.timer[which];
  849.  
  850.     /* if we have a timing timer running, adjust the count */
  851.     if (t->time_timer)
  852.     {
  853.         double current_time = timer_timeelapsed(t->time_timer);
  854.         int net_clocks = (int)((current_time - t->last_time) * 2000000.);
  855.         t->last_time = current_time;
  856.  
  857.         /* set the max count bit if we passed the max */
  858.         if ((int)t->count + net_clocks >= t->maxA)
  859.             t->control |= 0x0020;
  860.  
  861.         /* set the new count */
  862.         if (t->maxA != 0)
  863.             t->count = (t->count + net_clocks) % t->maxA;
  864.         else
  865.             t->count = t->count + net_clocks;
  866.     }
  867. }
  868.  
  869.  
  870. static void internal_timer_update(int which, int new_count, int new_maxA, int new_maxB, int new_control)
  871. {
  872.     struct timer_state *t = &i186.timer[which];
  873.     int update_int_timer = 0;
  874.  
  875.     /* if we have a new count and we're on, update things */
  876.     if (new_count != -1)
  877.     {
  878.         if (t->control & 0x8000)
  879.         {
  880.             internal_timer_sync(which);
  881.             update_int_timer = 1;
  882.         }
  883.         t->count = new_count;
  884.     }
  885.  
  886.     /* if we have a new max and we're on, update things */
  887.     if (new_maxA != -1 && new_maxA != t->maxA)
  888.     {
  889.         if (t->control & 0x8000)
  890.         {
  891.             internal_timer_sync(which);
  892.             update_int_timer = 1;
  893.         }
  894.         t->maxA = new_maxA;
  895.         if (new_maxA == 0) new_maxA = 0x10000;
  896.  
  897.         /* redline racer controls nothing externally? */
  898.         if (is_redline)
  899.             ;
  900.  
  901.         /* on the common board, timer 0 controls the 10-bit DAC frequency */
  902.         else if (which == 0)
  903.             set_dac_frequency(6, 2000000 / new_maxA);
  904.  
  905.         /* timer 1 controls the externally driven DAC on Indy Heat/WSF */
  906.         else if (which == 1 && has_ym2151)
  907.             set_dac_frequency(7, 2000000 / (new_maxA * 2));
  908.     }
  909.  
  910.     /* if we have a new max and we're on, update things */
  911.     if (new_maxB != -1 && new_maxB != t->maxB)
  912.     {
  913.         if (t->control & 0x8000)
  914.         {
  915.             internal_timer_sync(which);
  916.             update_int_timer = 1;
  917.         }
  918.         t->maxB = new_maxB;
  919.         if (new_maxB == 0) new_maxB = 0x10000;
  920.  
  921.         /* timer 1 controls the externally driven DAC on Indy Heat/WSF */
  922.         /* they alternate the use of maxA and maxB in a way that makes no */
  923.         /* sense according to the 80186 documentation! */
  924.         if (which == 1 && has_ym2151)
  925.             set_dac_frequency(7, 2000000 / (new_maxB * 2));
  926.     }
  927.  
  928.     /* handle control changes */
  929.     if (new_control != -1)
  930.     {
  931.         int diff;
  932.  
  933.         /* merge back in the bits we don't modify */
  934.         new_control = (new_control & ~0x1fc0) | (t->control & 0x1fc0);
  935.  
  936.         /* handle the /INH bit */
  937.         if (!(new_control & 0x4000))
  938.             new_control = (new_control & ~0x8000) | (t->control & 0x8000);
  939.         new_control &= ~0x4000;
  940.  
  941.         /* check for control bits we don't handle */
  942.         diff = new_control ^ t->control;
  943.         if (diff & 0x001c)
  944.             logerror("%05X:ERROR! - unsupported timer mode %04X\n", new_control);
  945.  
  946.         /* if we have real changes, update things */
  947.         if (diff != 0)
  948.         {
  949.             /* if we're going off, make sure our timers are gone */
  950.             if ((diff & 0x8000) && !(new_control & 0x8000))
  951.             {
  952.                 /* compute the final count */
  953.                 internal_timer_sync(which);
  954.  
  955.                 /* nuke the timer and force the interrupt timer to be recomputed */
  956.                 if (t->time_timer)
  957.                     timer_remove(t->time_timer);
  958.                 t->time_timer = NULL;
  959.                 update_int_timer = 1;
  960.             }
  961.  
  962.             /* if we're going on, start the timers running */
  963.             else if ((diff & 0x8000) && (new_control & 0x8000))
  964.             {
  965.                 /* start the timing */
  966.                 t->time_timer = timer_set(TIME_NEVER, 0, NULL);
  967.                 update_int_timer = 1;
  968.             }
  969.  
  970.             /* if something about the interrupt timer changed, force an update */
  971.             if (!(diff & 0x8000) && (diff & 0x2000))
  972.             {
  973.                 internal_timer_sync(which);
  974.                 update_int_timer = 1;
  975.             }
  976.         }
  977.  
  978.         /* set the new control register */
  979.         t->control = new_control;
  980.     }
  981.  
  982.     /* update the interrupt timer */
  983.  
  984.     /* kludge: the YM2151 games sometimes crank timer 1 really high, and leave interrupts */
  985.     /* enabled, even though the handler for timer 1 does nothing. To alleviate this, we */
  986.     /* just ignore it */
  987.     if (!has_ym2151 || which != 1)
  988.         if (update_int_timer)
  989.         {
  990.             if (t->int_timer)
  991.                 timer_remove(t->int_timer);
  992.             if ((t->control & 0x8000) && (t->control & 0x2000))
  993.             {
  994.                 int diff = t->maxA - t->count;
  995.                 if (diff <= 0) diff += 0x10000;
  996.                 t->int_timer = timer_set((double)diff * TIME_IN_HZ(2000000), which, internal_timer_int);
  997.                 if (LOG_TIMER) logerror("Set interrupt timer for %d\n", which);
  998.             }
  999.             else
  1000.                 t->int_timer = NULL;
  1001.         }
  1002. }
  1003.  
  1004.  
  1005.  
  1006. /*************************************
  1007.  *
  1008.  *    80186 internal DMA
  1009.  *
  1010.  *************************************/
  1011.  
  1012. static void dma_timer_callback(int which)
  1013. {
  1014.     struct dma_state *d = &i186.dma[which];
  1015.  
  1016.     /* force an update and see if we're really done */
  1017.     stream_update(dma_stream, 0);
  1018.  
  1019.     /* complete the status update */
  1020.     d->control &= ~0x0002;
  1021.     d->source += d->count;
  1022.     d->count = 0;
  1023.  
  1024.     /* check for interrupt generation */
  1025.     if (d->control & 0x0100)
  1026.     {
  1027.         if (LOG_DMA) logerror("DMA%d timer callback - requesting interrupt: count = %04X, source = %04X\n", which, d->count, d->source);
  1028.         i186.intr.request |= 0x04 << which;
  1029.         update_interrupt_state();
  1030.     }
  1031.     d->finish_timer = NULL;
  1032. }
  1033.  
  1034.  
  1035. static void update_dma_control(int which, int new_control)
  1036. {
  1037.     struct dma_state *d = &i186.dma[which];
  1038.     int diff;
  1039.  
  1040.     /* handle the CHG bit */
  1041.     if (!(new_control & 0x0004))
  1042.         new_control = (new_control & ~0x0002) | (d->control & 0x0002);
  1043.     new_control &= ~0x0004;
  1044.  
  1045.     /* check for control bits we don't handle */
  1046.     diff = new_control ^ d->control;
  1047.     if (diff & 0x6811)
  1048.         logerror("%05X:ERROR! - unsupported DMA mode %04X\n", new_control);
  1049.  
  1050.     /* if we're going live, set a timer */
  1051.     if ((diff & 0x0002) && (new_control & 0x0002))
  1052.     {
  1053.         /* make sure the parameters meet our expectations */
  1054.         if ((new_control & 0xfe00) != 0x1600)
  1055.         {
  1056.             logerror("Unexpected DMA control %02X\n", new_control);
  1057.         }
  1058.         else if (!is_redline && ((d->dest & 1) || (d->dest & 0x3f) > 0x0b))
  1059.         {
  1060.             logerror("Unexpected DMA destination %02X\n", d->dest);
  1061.         }
  1062.         else if (is_redline && (d->dest & 0xf000) != 0x4000 && (d->dest & 0xf000) != 0x5000)
  1063.         {
  1064.             logerror("Unexpected DMA destination %02X\n", d->dest);
  1065.         }
  1066.  
  1067.         /* otherwise, set a timer */
  1068.         else
  1069.         {
  1070.             int count = d->count;
  1071.             int dacnum;
  1072.  
  1073.             /* adjust for redline racer */
  1074.             if (!is_redline)
  1075.                 dacnum = (d->dest & 0x3f) / 2;
  1076.             else
  1077.             {
  1078.                 dacnum = (d->dest >> 9) & 7;
  1079.                 dac[dacnum].volume = (d->dest & 0x1fe) / 2 / DAC_VOLUME_SCALE;
  1080.             }
  1081.  
  1082.             if (LOG_DMA) logerror("Initiated DMA %d - count = %04X, source = %04X, dest = %04X\n", which, d->count, d->source, d->dest);
  1083.  
  1084.             if (d->finish_timer)
  1085.                 timer_remove(d->finish_timer);
  1086.             d->finished = 0;
  1087.             d->finish_timer = timer_set(TIME_IN_HZ(dac[dacnum].frequency) * (double)count, which, dma_timer_callback);
  1088.         }
  1089.     }
  1090.  
  1091.     /* set the new control register */
  1092.     d->control = new_control;
  1093. }
  1094.  
  1095.  
  1096.  
  1097. /*************************************
  1098.  *
  1099.  *    80186 internal I/O reads
  1100.  *
  1101.  *************************************/
  1102.  
  1103. static READ_HANDLER( i186_internal_port_r )
  1104. {
  1105.     int shift = 8 * (offset & 1);
  1106.     int temp, which;
  1107.  
  1108.     switch (offset & ~1)
  1109.     {
  1110.         case 0x22:
  1111.             logerror("%05X:ERROR - read from 80186 EOI\n", cpu_get_pc());
  1112.             break;
  1113.  
  1114.         case 0x24:
  1115.             if (LOG_PORTS) logerror("%05X:read 80186 interrupt poll\n", cpu_get_pc());
  1116.             if (i186.intr.poll_status & 0x8000)
  1117.                 int_callback(0);
  1118.             return (i186.intr.poll_status >> shift) & 0xff;
  1119.  
  1120.         case 0x26:
  1121.             if (LOG_PORTS) logerror("%05X:read 80186 interrupt poll status\n", cpu_get_pc());
  1122.             return (i186.intr.poll_status >> shift) & 0xff;
  1123.  
  1124.         case 0x28:
  1125.             if (LOG_PORTS) logerror("%05X:read 80186 interrupt mask\n", cpu_get_pc());
  1126.             temp  = (i186.intr.timer  >> 3) & 0x01;
  1127.             temp |= (i186.intr.dma[0] >> 1) & 0x04;
  1128.             temp |= (i186.intr.dma[1] >> 0) & 0x08;
  1129.             temp |= (i186.intr.ext[0] << 1) & 0x10;
  1130.             temp |= (i186.intr.ext[1] << 2) & 0x20;
  1131.             temp |= (i186.intr.ext[2] << 3) & 0x40;
  1132.             temp |= (i186.intr.ext[3] << 4) & 0x80;
  1133.             return (temp >> shift) & 0xff;
  1134.  
  1135.         case 0x2a:
  1136.             if (LOG_PORTS) logerror("%05X:read 80186 interrupt priority mask\n", cpu_get_pc());
  1137.             return (i186.intr.priority_mask >> shift) & 0xff;
  1138.  
  1139.         case 0x2c:
  1140.             if (LOG_PORTS) logerror("%05X:read 80186 interrupt in-service\n", cpu_get_pc());
  1141.             return (i186.intr.in_service >> shift) & 0xff;
  1142.  
  1143.         case 0x2e:
  1144.             if (LOG_PORTS) logerror("%05X:read 80186 interrupt request\n", cpu_get_pc());
  1145.             temp = i186.intr.request & ~0x0001;
  1146.             if (i186.intr.status & 0x0007)
  1147.                 temp |= 1;
  1148.             return (temp >> shift) & 0xff;
  1149.  
  1150.         case 0x30:
  1151.             if (LOG_PORTS) logerror("%05X:read 80186 interrupt status\n", cpu_get_pc());
  1152.             return (i186.intr.status >> shift) & 0xff;
  1153.  
  1154.         case 0x32:
  1155.             if (LOG_PORTS) logerror("%05X:read 80186 timer interrupt control\n", cpu_get_pc());
  1156.             return (i186.intr.timer >> shift) & 0xff;
  1157.  
  1158.         case 0x34:
  1159.             if (LOG_PORTS) logerror("%05X:read 80186 DMA 0 interrupt control\n", cpu_get_pc());
  1160.             return (i186.intr.dma[0] >> shift) & 0xff;
  1161.  
  1162.         case 0x36:
  1163.             if (LOG_PORTS) logerror("%05X:read 80186 DMA 1 interrupt control\n", cpu_get_pc());
  1164.             return (i186.intr.dma[1] >> shift) & 0xff;
  1165.  
  1166.         case 0x38:
  1167.             if (LOG_PORTS) logerror("%05X:read 80186 INT 0 interrupt control\n", cpu_get_pc());
  1168.             return (i186.intr.ext[0] >> shift) & 0xff;
  1169.  
  1170.         case 0x3a:
  1171.             if (LOG_PORTS) logerror("%05X:read 80186 INT 1 interrupt control\n", cpu_get_pc());
  1172.             return (i186.intr.ext[1] >> shift) & 0xff;
  1173.  
  1174.         case 0x3c:
  1175.             if (LOG_PORTS) logerror("%05X:read 80186 INT 2 interrupt control\n", cpu_get_pc());
  1176.             return (i186.intr.ext[2] >> shift) & 0xff;
  1177.  
  1178.         case 0x3e:
  1179.             if (LOG_PORTS) logerror("%05X:read 80186 INT 3 interrupt control\n", cpu_get_pc());
  1180.             return (i186.intr.ext[3] >> shift) & 0xff;
  1181.  
  1182.         case 0x50:
  1183.         case 0x58:
  1184.         case 0x60:
  1185.             if (LOG_PORTS) logerror("%05X:read 80186 Timer %d count\n", cpu_get_pc(), (offset - 0x50) / 8);
  1186.             which = (offset - 0x50) / 8;
  1187.             if (!(offset & 1))
  1188.                 internal_timer_sync(which);
  1189.             return (i186.timer[which].count >> shift) & 0xff;
  1190.  
  1191.         case 0x52:
  1192.         case 0x5a:
  1193.         case 0x62:
  1194.             if (LOG_PORTS) logerror("%05X:read 80186 Timer %d max A\n", cpu_get_pc(), (offset - 0x50) / 8);
  1195.             which = (offset - 0x50) / 8;
  1196.             return (i186.timer[which].maxA >> shift) & 0xff;
  1197.  
  1198.         case 0x54:
  1199.         case 0x5c:
  1200.             logerror("%05X:read 80186 Timer %d max B\n", cpu_get_pc(), (offset - 0x50) / 8);
  1201.             which = (offset - 0x50) / 8;
  1202.             return (i186.timer[which].maxB >> shift) & 0xff;
  1203.  
  1204.         case 0x56:
  1205.         case 0x5e:
  1206.         case 0x66:
  1207.             if (LOG_PORTS) logerror("%05X:read 80186 Timer %d control\n", cpu_get_pc(), (offset - 0x50) / 8);
  1208.             which = (offset - 0x50) / 8;
  1209.             return (i186.timer[which].control >> shift) & 0xff;
  1210.  
  1211.         case 0xa0:
  1212.             if (LOG_PORTS) logerror("%05X:read 80186 upper chip select\n", cpu_get_pc());
  1213.             return (i186.mem.upper >> shift) & 0xff;
  1214.  
  1215.         case 0xa2:
  1216.             if (LOG_PORTS) logerror("%05X:read 80186 lower chip select\n", cpu_get_pc());
  1217.             return (i186.mem.lower >> shift) & 0xff;
  1218.  
  1219.         case 0xa4:
  1220.             if (LOG_PORTS) logerror("%05X:read 80186 peripheral chip select\n", cpu_get_pc());
  1221.             return (i186.mem.peripheral >> shift) & 0xff;
  1222.  
  1223.         case 0xa6:
  1224.             if (LOG_PORTS) logerror("%05X:read 80186 middle chip select\n", cpu_get_pc());
  1225.             return (i186.mem.middle >> shift) & 0xff;
  1226.  
  1227.         case 0xa8:
  1228.             if (LOG_PORTS) logerror("%05X:read 80186 middle P chip select\n", cpu_get_pc());
  1229.             return (i186.mem.middle_size >> shift) & 0xff;
  1230.  
  1231.         case 0xc0:
  1232.         case 0xd0:
  1233.             if (LOG_PORTS) logerror("%05X:read 80186 DMA%d lower source address\n", cpu_get_pc(), (offset - 0xc0) / 0x10);
  1234.             which = (offset - 0xc0) / 0x10;
  1235.             stream_update(dma_stream, 0);
  1236.             return (i186.dma[which].source >> shift) & 0xff;
  1237.  
  1238.         case 0xc2:
  1239.         case 0xd2:
  1240.             if (LOG_PORTS) logerror("%05X:read 80186 DMA%d upper source address\n", cpu_get_pc(), (offset - 0xc0) / 0x10);
  1241.             which = (offset - 0xc0) / 0x10;
  1242.             stream_update(dma_stream, 0);
  1243.             return (i186.dma[which].source >> (shift + 16)) & 0xff;
  1244.  
  1245.         case 0xc4:
  1246.         case 0xd4:
  1247.             if (LOG_PORTS) logerror("%05X:read 80186 DMA%d lower dest address\n", cpu_get_pc(), (offset - 0xc0) / 0x10);
  1248.             which = (offset - 0xc0) / 0x10;
  1249.             stream_update(dma_stream, 0);
  1250.             return (i186.dma[which].dest >> shift) & 0xff;
  1251.  
  1252.         case 0xc6:
  1253.         case 0xd6:
  1254.             if (LOG_PORTS) logerror("%05X:read 80186 DMA%d upper dest address\n", cpu_get_pc(), (offset - 0xc0) / 0x10);
  1255.             which = (offset - 0xc0) / 0x10;
  1256.             stream_update(dma_stream, 0);
  1257.             return (i186.dma[which].dest >> (shift + 16)) & 0xff;
  1258.  
  1259.         case 0xc8:
  1260.         case 0xd8:
  1261.             if (LOG_PORTS) logerror("%05X:read 80186 DMA%d transfer count\n", cpu_get_pc(), (offset - 0xc0) / 0x10);
  1262.             which = (offset - 0xc0) / 0x10;
  1263.             stream_update(dma_stream, 0);
  1264.             return (i186.dma[which].count >> shift) & 0xff;
  1265.  
  1266.         case 0xca:
  1267.         case 0xda:
  1268.             if (LOG_PORTS) logerror("%05X:read 80186 DMA%d control\n", cpu_get_pc(), (offset - 0xc0) / 0x10);
  1269.             which = (offset - 0xc0) / 0x10;
  1270.             stream_update(dma_stream, 0);
  1271.             return (i186.dma[which].control >> shift) & 0xff;
  1272.  
  1273.         default:
  1274.             logerror("%05X:read 80186 port %02X\n", cpu_get_pc(), offset);
  1275.             break;
  1276.     }
  1277.     return 0x00;
  1278. }
  1279.  
  1280.  
  1281.  
  1282. /*************************************
  1283.  *
  1284.  *    80186 internal I/O writes
  1285.  *
  1286.  *************************************/
  1287.  
  1288. static WRITE_HANDLER( i186_internal_port_w )
  1289. {
  1290.     static UINT8 even_byte;
  1291.     int temp, which;
  1292.  
  1293.     /* warning: this assumes all port writes here are word-sized */
  1294.     if (!(offset & 1))
  1295.     {
  1296.         even_byte = data;
  1297.         return;
  1298.     }
  1299.     data = ((data & 0xff) << 8) | even_byte;
  1300.  
  1301.     switch (offset & ~1)
  1302.     {
  1303.         case 0x22:
  1304.             if (LOG_PORTS) logerror("%05X:80186 EOI = %04X\n", cpu_get_pc(), data);
  1305.             handle_eoi(0x8000);
  1306.             update_interrupt_state();
  1307.             break;
  1308.  
  1309.         case 0x24:
  1310.             logerror("%05X:ERROR - write to 80186 interrupt poll = %04X\n", cpu_get_pc(), data);
  1311.             break;
  1312.  
  1313.         case 0x26:
  1314.             logerror("%05X:ERROR - write to 80186 interrupt poll status = %04X\n", cpu_get_pc(), data);
  1315.             break;
  1316.  
  1317.         case 0x28:
  1318.             if (LOG_PORTS) logerror("%05X:80186 interrupt mask = %04X\n", cpu_get_pc(), data);
  1319.             i186.intr.timer  = (i186.intr.timer  & ~0x08) | ((data << 3) & 0x08);
  1320.             i186.intr.dma[0] = (i186.intr.dma[0] & ~0x08) | ((data << 1) & 0x08);
  1321.             i186.intr.dma[1] = (i186.intr.dma[1] & ~0x08) | ((data << 0) & 0x08);
  1322.             i186.intr.ext[0] = (i186.intr.ext[0] & ~0x08) | ((data >> 1) & 0x08);
  1323.             i186.intr.ext[1] = (i186.intr.ext[1] & ~0x08) | ((data >> 2) & 0x08);
  1324.             i186.intr.ext[2] = (i186.intr.ext[2] & ~0x08) | ((data >> 3) & 0x08);
  1325.             i186.intr.ext[3] = (i186.intr.ext[3] & ~0x08) | ((data >> 4) & 0x08);
  1326.             update_interrupt_state();
  1327.             break;
  1328.  
  1329.         case 0x2a:
  1330.             if (LOG_PORTS) logerror("%05X:80186 interrupt priority mask = %04X\n", cpu_get_pc(), data);
  1331.             i186.intr.priority_mask = data & 0x0007;
  1332.             update_interrupt_state();
  1333.             break;
  1334.  
  1335.         case 0x2c:
  1336.             if (LOG_PORTS) logerror("%05X:80186 interrupt in-service = %04X\n", cpu_get_pc(), data);
  1337.             i186.intr.in_service = data & 0x00ff;
  1338.             update_interrupt_state();
  1339.             break;
  1340.  
  1341.         case 0x2e:
  1342.             if (LOG_PORTS) logerror("%05X:80186 interrupt request = %04X\n", cpu_get_pc(), data);
  1343.             i186.intr.request = (i186.intr.request & ~0x00c0) | (data & 0x00c0);
  1344.             update_interrupt_state();
  1345.             break;
  1346.  
  1347.         case 0x30:
  1348.             if (LOG_PORTS) logerror("%05X:WARNING - wrote to 80186 interrupt status = %04X\n", cpu_get_pc(), data);
  1349.             i186.intr.status = (i186.intr.status & ~0x8007) | (data & 0x8007);
  1350.             update_interrupt_state();
  1351.             break;
  1352.  
  1353.         case 0x32:
  1354.             if (LOG_PORTS) logerror("%05X:80186 timer interrupt contol = %04X\n", cpu_get_pc(), data);
  1355.             i186.intr.timer = data & 0x000f;
  1356.             break;
  1357.  
  1358.         case 0x34:
  1359.             if (LOG_PORTS) logerror("%05X:80186 DMA 0 interrupt control = %04X\n", cpu_get_pc(), data);
  1360.             i186.intr.dma[0] = data & 0x000f;
  1361.             break;
  1362.  
  1363.         case 0x36:
  1364.             if (LOG_PORTS) logerror("%05X:80186 DMA 1 interrupt control = %04X\n", cpu_get_pc(), data);
  1365.             i186.intr.dma[1] = data & 0x000f;
  1366.             break;
  1367.  
  1368.         case 0x38:
  1369.             if (LOG_PORTS) logerror("%05X:80186 INT 0 interrupt control = %04X\n", cpu_get_pc(), data);
  1370.             i186.intr.ext[0] = data & 0x007f;
  1371.             break;
  1372.  
  1373.         case 0x3a:
  1374.             if (LOG_PORTS) logerror("%05X:80186 INT 1 interrupt control = %04X\n", cpu_get_pc(), data);
  1375.             i186.intr.ext[1] = data & 0x007f;
  1376.             break;
  1377.  
  1378.         case 0x3c:
  1379.             if (LOG_PORTS) logerror("%05X:80186 INT 2 interrupt control = %04X\n", cpu_get_pc(), data);
  1380.             i186.intr.ext[2] = data & 0x001f;
  1381.             break;
  1382.  
  1383.         case 0x3e:
  1384.             if (LOG_PORTS) logerror("%05X:80186 INT 3 interrupt control = %04X\n", cpu_get_pc(), data);
  1385.             i186.intr.ext[3] = data & 0x001f;
  1386.             break;
  1387.  
  1388.         case 0x50:
  1389.         case 0x58:
  1390.         case 0x60:
  1391.             if (LOG_PORTS) logerror("%05X:80186 Timer %d count = %04X\n", cpu_get_pc(), (offset - 0x50) / 8, data);
  1392.             which = (offset - 0x50) / 8;
  1393.             internal_timer_update(which, data, -1, -1, -1);
  1394.             break;
  1395.  
  1396.         case 0x52:
  1397.         case 0x5a:
  1398.         case 0x62:
  1399.             if (LOG_PORTS) logerror("%05X:80186 Timer %d max A = %04X\n", cpu_get_pc(), (offset - 0x50) / 8, data);
  1400.             which = (offset - 0x50) / 8;
  1401.             internal_timer_update(which, -1, data, -1, -1);
  1402.             break;
  1403.  
  1404.         case 0x54:
  1405.         case 0x5c:
  1406.             if (LOG_PORTS) logerror("%05X:80186 Timer %d max B = %04X\n", cpu_get_pc(), (offset - 0x50) / 8, data);
  1407.             which = (offset - 0x50) / 8;
  1408.             internal_timer_update(which, -1, -1, data, -1);
  1409.             break;
  1410.  
  1411.         case 0x56:
  1412.         case 0x5e:
  1413.         case 0x66:
  1414.             if (LOG_PORTS) logerror("%05X:80186 Timer %d control = %04X\n", cpu_get_pc(), (offset - 0x50) / 8, data);
  1415.             which = (offset - 0x50) / 8;
  1416.             internal_timer_update(which, -1, -1, -1, data);
  1417.             break;
  1418.  
  1419.         case 0xa0:
  1420.             if (LOG_PORTS) logerror("%05X:80186 upper chip select = %04X\n", cpu_get_pc(), data);
  1421.             i186.mem.upper = data | 0xc038;
  1422.             break;
  1423.  
  1424.         case 0xa2:
  1425.             if (LOG_PORTS) logerror("%05X:80186 lower chip select = %04X\n", cpu_get_pc(), data);
  1426.             i186.mem.lower = (data & 0x3fff) | 0x0038;
  1427.             break;
  1428.  
  1429.         case 0xa4:
  1430.             if (LOG_PORTS) logerror("%05X:80186 peripheral chip select = %04X\n", cpu_get_pc(), data);
  1431.             i186.mem.peripheral = data | 0x0038;
  1432.             break;
  1433.  
  1434.         case 0xa6:
  1435.             if (LOG_PORTS) logerror("%05X:80186 middle chip select = %04X\n", cpu_get_pc(), data);
  1436.             i186.mem.middle = data | 0x01f8;
  1437.             break;
  1438.  
  1439.         case 0xa8:
  1440.             if (LOG_PORTS) logerror("%05X:80186 middle P chip select = %04X\n", cpu_get_pc(), data);
  1441.             i186.mem.middle_size = data | 0x8038;
  1442.  
  1443.             temp = (i186.mem.peripheral & 0xffc0) << 4;
  1444.             if (i186.mem.middle_size & 0x0040)
  1445.             {
  1446.                 install_mem_read_handler(2, temp, temp + 0x2ff, peripheral_r);
  1447.                 install_mem_write_handler(2, temp, temp + 0x2ff, peripheral_w);
  1448.             }
  1449.             else
  1450.             {
  1451.                 temp &= 0xffff;
  1452.                 install_port_read_handler(2, temp, temp + 0x2ff, peripheral_r);
  1453.                 install_port_write_handler(2, temp, temp + 0x2ff, peripheral_w);
  1454.             }
  1455.  
  1456.             /* we need to do this at a time when the I86 context is swapped in */
  1457.             /* this register is generally set once at startup and never again, so it's a good */
  1458.             /* time to set it up */
  1459.             i86_set_irq_callback(int_callback);
  1460.             break;
  1461.  
  1462.         case 0xc0:
  1463.         case 0xd0:
  1464.             if (LOG_PORTS) logerror("%05X:80186 DMA%d lower source address = %04X\n", cpu_get_pc(), (offset - 0xc0) / 0x10, data);
  1465.             which = (offset - 0xc0) / 0x10;
  1466.             stream_update(dma_stream, 0);
  1467.             i186.dma[which].source = (i186.dma[which].source & ~0x0ffff) | (data & 0x0ffff);
  1468.             break;
  1469.  
  1470.         case 0xc2:
  1471.         case 0xd2:
  1472.             if (LOG_PORTS) logerror("%05X:80186 DMA%d upper source address = %04X\n", cpu_get_pc(), (offset - 0xc0) / 0x10, data);
  1473.             which = (offset - 0xc0) / 0x10;
  1474.             stream_update(dma_stream, 0);
  1475.             i186.dma[which].source = (i186.dma[which].source & ~0xf0000) | ((data << 16) & 0xf0000);
  1476.             break;
  1477.  
  1478.         case 0xc4:
  1479.         case 0xd4:
  1480.             if (LOG_PORTS) logerror("%05X:80186 DMA%d lower dest address = %04X\n", cpu_get_pc(), (offset - 0xc0) / 0x10, data);
  1481.             which = (offset - 0xc0) / 0x10;
  1482.             stream_update(dma_stream, 0);
  1483.             i186.dma[which].dest = (i186.dma[which].dest & ~0x0ffff) | (data & 0x0ffff);
  1484.             break;
  1485.  
  1486.         case 0xc6:
  1487.         case 0xd6:
  1488.             if (LOG_PORTS) logerror("%05X:80186 DMA%d upper dest address = %04X\n", cpu_get_pc(), (offset - 0xc0) / 0x10, data);
  1489.             which = (offset - 0xc0) / 0x10;
  1490.             stream_update(dma_stream, 0);
  1491.             i186.dma[which].dest = (i186.dma[which].dest & ~0xf0000) | ((data << 16) & 0xf0000);
  1492.             break;
  1493.  
  1494.         case 0xc8:
  1495.         case 0xd8:
  1496.             if (LOG_PORTS) logerror("%05X:80186 DMA%d transfer count = %04X\n", cpu_get_pc(), (offset - 0xc0) / 0x10, data);
  1497.             which = (offset - 0xc0) / 0x10;
  1498.             stream_update(dma_stream, 0);
  1499.             i186.dma[which].count = data;
  1500.             break;
  1501.  
  1502.         case 0xca:
  1503.         case 0xda:
  1504.             if (LOG_PORTS) logerror("%05X:80186 DMA%d control = %04X\n", cpu_get_pc(), (offset - 0xc0) / 0x10, data);
  1505.             which = (offset - 0xc0) / 0x10;
  1506.             stream_update(dma_stream, 0);
  1507.             update_dma_control(which, data);
  1508.             break;
  1509.  
  1510.         case 0xfe:
  1511.             if (LOG_PORTS) logerror("%05X:80186 relocation register = %04X\n", cpu_get_pc(), data);
  1512.  
  1513.             /* we assume here there that this doesn't happen too often */
  1514.             /* plus, we can't really remove the old memory range, so we also assume that it's */
  1515.             /* okay to leave us mapped where we were */
  1516.             temp = (data & 0x0fff) << 8;
  1517.             if (data & 0x1000)
  1518.             {
  1519.                 install_mem_read_handler(2, temp, temp + 0xff, i186_internal_port_r);
  1520.                 install_mem_write_handler(2, temp, temp + 0xff, i186_internal_port_w);
  1521.             }
  1522.             else
  1523.             {
  1524.                 temp &= 0xffff;
  1525.                 install_port_read_handler(2, temp, temp + 0xff, i186_internal_port_r);
  1526.                 install_port_write_handler(2, temp, temp + 0xff, i186_internal_port_w);
  1527.             }
  1528. /*            usrintf_showmessage("Sound CPU reset");*/
  1529.             break;
  1530.  
  1531.         default:
  1532.             logerror("%05X:80186 port %02X = %04X\n", cpu_get_pc(), offset, data);
  1533.             break;
  1534.     }
  1535. }
  1536.  
  1537.  
  1538.  
  1539. /*************************************
  1540.  *
  1541.  *    8254 PIT accesses
  1542.  *
  1543.  *************************************/
  1544.  
  1545. INLINE void counter_update_count(int which)
  1546. {
  1547.     /* only update if the timer is running */
  1548.     if (counter[which].timer)
  1549.     {
  1550.         /* determine how many 2MHz cycles are remaining */
  1551.         int count = (int)(timer_timeleft(counter[which].timer) / TIME_IN_HZ(2000000));
  1552.         counter[which].count = (count < 0) ? 0 : count;
  1553.     }
  1554. }
  1555.  
  1556.  
  1557. static READ_HANDLER( pit8254_r )
  1558. {
  1559.     struct counter_state *ctr;
  1560.     int which = offset / 0x80;
  1561.     int reg = (offset / 2) & 3;
  1562.  
  1563.     /* ignore odd offsets */
  1564.     if (offset & 1)
  1565.         return 0;
  1566.  
  1567.     /* switch off the register */
  1568.     switch (offset & 3)
  1569.     {
  1570.         case 0:
  1571.         case 1:
  1572.         case 2:
  1573.             /* warning: assumes LSB/MSB addressing and no latching! */
  1574.             which = (which * 3) + reg;
  1575.             ctr = &counter[which];
  1576.  
  1577.             /* update the count */
  1578.             counter_update_count(which);
  1579.  
  1580.             /* return the LSB */
  1581.             if (counter[which].readbyte == 0)
  1582.             {
  1583.                 counter[which].readbyte = 1;
  1584.                 return counter[which].count & 0xff;
  1585.             }
  1586.  
  1587.             /* write the MSB and reset the counter */
  1588.             else
  1589.             {
  1590.                 counter[which].readbyte = 0;
  1591.                 return (counter[which].count >> 8) & 0xff;
  1592.             }
  1593.             break;
  1594.     }
  1595.     return 0;
  1596. }
  1597.  
  1598.  
  1599. static WRITE_HANDLER( pit8254_w )
  1600. {
  1601.     struct counter_state *ctr;
  1602.     int which = offset / 0x80;
  1603.     int reg = (offset / 2) & 3;
  1604.  
  1605.     /* ignore odd offsets */
  1606.     if (offset & 1)
  1607.         return;
  1608.  
  1609.     /* switch off the register */
  1610.     switch (reg)
  1611.     {
  1612.         case 0:
  1613.         case 1:
  1614.         case 2:
  1615.             /* warning: assumes LSB/MSB addressing and no latching! */
  1616.             which = (which * 3) + reg;
  1617.             ctr = &counter[which];
  1618.  
  1619.             /* write the LSB */
  1620.             if (ctr->writebyte == 0)
  1621.             {
  1622.                 ctr->count = (ctr->count & 0xff00) | (data & 0x00ff);
  1623.                 ctr->writebyte = 1;
  1624.             }
  1625.  
  1626.             /* write the MSB and reset the counter */
  1627.             else
  1628.             {
  1629.                 ctr->count = (ctr->count & 0x00ff) | ((data << 8) & 0xff00);
  1630.                 ctr->writebyte = 0;
  1631.  
  1632.                 /* treat 0 as $10000 */
  1633.                 if (ctr->count == 0) ctr->count = 0x10000;
  1634.  
  1635.                 /* reset/start the timer */
  1636.                 if (ctr->timer)
  1637.                     timer_reset(ctr->timer, TIME_NEVER);
  1638.                 else
  1639.                     ctr->timer = timer_set(TIME_NEVER, 0, NULL);
  1640.  
  1641.                 if (LOG_PIT) logerror("PIT counter %d set to %d (%d Hz)\n", which, ctr->count, 4000000 / ctr->count);
  1642.  
  1643.                 /* set the frequency of the associated DAC */
  1644.                 if (!is_redline)
  1645.                     set_dac_frequency(which, 4000000 / ctr->count);
  1646.                 else
  1647.                 {
  1648.                     if (which < 5)
  1649.                         set_dac_frequency(which, 7000000 / ctr->count);
  1650.                     else if (which == 6)
  1651.                     {
  1652.                         set_dac_frequency(5, 7000000 / ctr->count);
  1653.                         set_dac_frequency(6, 7000000 / ctr->count);
  1654.                         set_dac_frequency(7, 7000000 / ctr->count);
  1655.                     }
  1656.                 }
  1657.             }
  1658.             break;
  1659.  
  1660.         case 3:
  1661.             /* determine which counter */
  1662.             if ((data & 0xc0) == 0xc0) break;
  1663.             which = (which * 3) + (data >> 6);
  1664.             ctr = &counter[which];
  1665.  
  1666.             /* set the mode */
  1667.             ctr->mode = (data >> 1) & 7;
  1668.             break;
  1669.     }
  1670. }
  1671.  
  1672.  
  1673.  
  1674. /*************************************
  1675.  *
  1676.  *    External 80186 control
  1677.  *
  1678.  *************************************/
  1679.  
  1680. WRITE_HANDLER( leland_i86_control_w )
  1681. {
  1682.     /* see if anything changed */
  1683.     int diff = (last_control ^ data) & 0xf8;
  1684.     if (!diff)
  1685.         return;
  1686.     last_control = data;
  1687.  
  1688.     if (LOG_COMM)
  1689.     {
  1690.         logerror("%04X:I86 control = %02X", cpu_getpreviouspc(), data);
  1691.         if (!(data & 0x80)) logerror("  /RESET");
  1692.         if (!(data & 0x40)) logerror("  ZNMI");
  1693.         if (!(data & 0x20)) logerror("  INT0");
  1694.         if (!(data & 0x10)) logerror("  /TEST");
  1695.         if (!(data & 0x08)) logerror("  INT1");
  1696.         logerror("\n");
  1697.     }
  1698.  
  1699.     /* /RESET */
  1700.     cpu_set_reset_line(2, data & 0x80  ? CLEAR_LINE : ASSERT_LINE);
  1701.  
  1702.     /* /NMI */
  1703. /*     If the master CPU doesn't get a response by the time it's ready to send
  1704.     the next command, it uses an NMI to force the issue; unfortunately, this
  1705.     seems to really screw up the sound system. It turns out it's better to
  1706.     just wait for the original interrupt to occur naturally */
  1707. /*    cpu_set_nmi_line  (2, data & 0x40  ? CLEAR_LINE : ASSERT_LINE);*/
  1708.  
  1709.     /* INT0 */
  1710.     if (data & 0x20)
  1711.     {
  1712.         if (!LATCH_INTS) i186.intr.request &= ~0x10;
  1713.     }
  1714.     else if (i186.intr.ext[0] & 0x10)
  1715.         i186.intr.request |= 0x10;
  1716.     else if (diff & 0x20)
  1717.         i186.intr.request |= 0x10;
  1718.  
  1719.     /* INT1 */
  1720.     if (data & 0x08)
  1721.     {
  1722.         if (!LATCH_INTS) i186.intr.request &= ~0x20;
  1723.     }
  1724.     else if (i186.intr.ext[1] & 0x10)
  1725.         i186.intr.request |= 0x20;
  1726.     else if (diff & 0x08)
  1727.         i186.intr.request |= 0x20;
  1728.  
  1729.     /* handle reset here */
  1730.     if ((diff & 0x80) && (data & 0x80))
  1731.         leland_i186_reset();
  1732.  
  1733.     update_interrupt_state();
  1734. }
  1735.  
  1736.  
  1737.  
  1738. /*************************************
  1739.  *
  1740.  *    Sound command handling
  1741.  *
  1742.  *************************************/
  1743.  
  1744. static void command_lo_sync(int data)
  1745. {
  1746.     if (LOG_COMM) logerror("%04X:Write sound command latch lo = %02X\n", cpu_getpreviouspc(), data);
  1747.     sound_command[0] = data;
  1748. }
  1749.  
  1750.  
  1751. WRITE_HANDLER( leland_i86_command_lo_w )
  1752. {
  1753.     timer_set(TIME_NOW, data, command_lo_sync);
  1754. }
  1755.  
  1756.  
  1757. WRITE_HANDLER( leland_i86_command_hi_w )
  1758. {
  1759.     if (LOG_COMM) logerror("%04X:Write sound command latch hi = %02X\n", cpu_getpreviouspc(), data);
  1760.     sound_command[1] = data;
  1761. }
  1762.  
  1763.  
  1764. static READ_HANDLER( main_to_sound_comm_r )
  1765. {
  1766.     if (!(offset & 1))
  1767.     {
  1768.         if (LOG_COMM) logerror("%05X:Read sound command latch lo = %02X\n", cpu_get_pc(), sound_command[0]);
  1769.         return sound_command[0];
  1770.     }
  1771.     else
  1772.     {
  1773.         if (LOG_COMM) logerror("%05X:Read sound command latch hi = %02X\n", cpu_get_pc(), sound_command[1]);
  1774.         return sound_command[1];
  1775.     }
  1776. }
  1777.  
  1778.  
  1779.  
  1780.  
  1781. /*************************************
  1782.  *
  1783.  *    Sound response handling
  1784.  *
  1785.  *************************************/
  1786.  
  1787. static void delayed_response_r(int checkpc)
  1788. {
  1789.     int pc = cpunum_get_reg(0, Z80_PC);
  1790.     int oldaf = cpunum_get_reg(0, Z80_AF);
  1791.  
  1792.     /* This is pretty cheesy, but necessary. Since the CPUs run in round-robin order,
  1793.        synchronizing on the write to this register from the slave side does nothing.
  1794.        In order to make sure the master CPU get the real response, we synchronize on
  1795.        the read. However, the value we returned the first time around may not be
  1796.        accurate, so after the system has synced up, we go back into the master CPUs
  1797.        state and put the proper value into the A register. */
  1798.     if (pc == checkpc)
  1799.     {
  1800.         if (LOG_COMM) logerror("(Updated sound response latch to %02X)\n", sound_response);
  1801.  
  1802.         oldaf = (oldaf & 0x00ff) | (sound_response << 8);
  1803.         cpunum_set_reg(0, Z80_AF, oldaf);
  1804.     }
  1805.     else
  1806.         logerror("ERROR: delayed_response_r - current PC = %04X, checkPC = %04X\n", pc, checkpc);
  1807. }
  1808.  
  1809.  
  1810. READ_HANDLER( leland_i86_response_r )
  1811. {
  1812.     if (LOG_COMM) logerror("%04X:Read sound response latch = %02X\n", cpu_getpreviouspc(), sound_response);
  1813.  
  1814.     /* if sound is disabled, toggle between FF and 00 */
  1815.     if (Machine->sample_rate == 0)
  1816.         return sound_response ^= 0xff;
  1817.     else
  1818.     {
  1819.         /* synchronize the response */
  1820.         timer_set(TIME_NOW, cpu_getpreviouspc() + 2, delayed_response_r);
  1821.         return sound_response;
  1822.     }
  1823. }
  1824.  
  1825.  
  1826. static WRITE_HANDLER( sound_to_main_comm_w )
  1827. {
  1828.     if (LOG_COMM) logerror("%05X:Write sound response latch = %02X\n", cpu_get_pc(), data);
  1829.     sound_response = data;
  1830. }
  1831.  
  1832.  
  1833.  
  1834. /*************************************
  1835.  *
  1836.  *    Low-level DAC I/O
  1837.  *
  1838.  *************************************/
  1839.  
  1840. static void set_dac_frequency(int which, int frequency)
  1841. {
  1842.     struct dac_state *d = &dac[which];
  1843.     int count = (d->bufin - d->bufout) & DAC_BUFFER_SIZE_MASK;
  1844.  
  1845.     /* set the frequency of the associated DAC */
  1846.     d->frequency = frequency;
  1847.     d->step = (int)((double)frequency * (double)(1 << 24) / (double)Machine->sample_rate);
  1848.  
  1849.     /* also determine the target buffer size */
  1850.     d->buftarget = dac[which].frequency / 60 + 50;
  1851.     if (d->buftarget > DAC_BUFFER_SIZE - 1)
  1852.         d->buftarget = DAC_BUFFER_SIZE - 1;
  1853.  
  1854.     /* reevaluate the count */
  1855.     if (count > d->buftarget)
  1856.         clock_active &= ~(1 << which);
  1857.     else if (count < d->buftarget)
  1858.     {
  1859.         if (LOG_OPTIMIZATION) logerror("  - trigger due to clock active in set_dac_frequency\n");
  1860.         cpu_trigger(CPU_RESUME_TRIGGER);
  1861.         clock_active |= 1 << which;
  1862.     }
  1863.  
  1864.     if (LOG_DAC) logerror("DAC %d frequency = %d, step = %08X\n", which, d->frequency, d->step);
  1865. }
  1866.  
  1867.  
  1868. static WRITE_HANDLER( dac_w )
  1869. {
  1870.     int which = offset / 2;
  1871.     struct dac_state *d = &dac[which];
  1872.  
  1873.     /* handle value changes */
  1874.     if (!(offset & 1))
  1875.     {
  1876.         int count = (d->bufin - d->bufout) & DAC_BUFFER_SIZE_MASK;
  1877.  
  1878.         /* set the new value */
  1879.         d->value = (INT16)data - 0x80;
  1880.         if (LOG_DAC) logerror("%05X:DAC %d value = %02X\n", cpu_get_pc(), offset / 2, data);
  1881.  
  1882.         /* if we haven't overflowed the buffer, add the value value to it */
  1883.         if (count < DAC_BUFFER_SIZE - 1)
  1884.         {
  1885.             /* if this is the first byte, sync the stream */
  1886.             if (count == 0)
  1887.                 stream_update(nondma_stream, 0);
  1888.  
  1889.             /* prescale by the volume */
  1890.             d->buffer[d->bufin] = d->value * d->volume;
  1891.             d->bufin = (d->bufin + 1) & DAC_BUFFER_SIZE_MASK;
  1892.  
  1893.             /* update the clock status */
  1894.             if (++count > d->buftarget)
  1895.                 clock_active &= ~(1 << which);
  1896.         }
  1897.     }
  1898.  
  1899.     /* handle volume changes */
  1900.     else
  1901.     {
  1902.         d->volume = (data ^ 0x00) / DAC_VOLUME_SCALE;
  1903.         if (LOG_DAC) logerror("%05X:DAC %d volume = %02X\n", cpu_get_pc(), offset / 2, data);
  1904.     }
  1905. }
  1906.  
  1907.  
  1908. static WRITE_HANDLER( redline_dac_w )
  1909. {
  1910.     int which = offset / 0x200;
  1911.     struct dac_state *d = &dac[which];
  1912.     int count = (d->bufin - d->bufout) & DAC_BUFFER_SIZE_MASK;
  1913.  
  1914.     /* set the new value */
  1915.     d->value = (INT16)data - 0x80;
  1916.  
  1917.     /* if we haven't overflowed the buffer, add the value value to it */
  1918.     if (count < DAC_BUFFER_SIZE - 1)
  1919.     {
  1920.         /* if this is the first byte, sync the stream */
  1921.         if (count == 0)
  1922.             stream_update(nondma_stream, 0);
  1923.  
  1924.         /* prescale by the volume */
  1925.         d->buffer[d->bufin] = d->value * d->volume;
  1926.         d->bufin = (d->bufin + 1) & DAC_BUFFER_SIZE_MASK;
  1927.  
  1928.         /* update the clock status */
  1929.         if (++count > d->buftarget)
  1930.             clock_active &= ~(1 << which);
  1931.     }
  1932.  
  1933.     /* update the volume */
  1934.     d->volume = (offset & 0x1fe) / 2 / DAC_VOLUME_SCALE;
  1935.     if (LOG_DAC) logerror("%05X:DAC %d value = %02X, volume = %02X\n", cpu_get_pc(), which, data, (offset & 0x1fe) / 2);
  1936. }
  1937.  
  1938.  
  1939. static WRITE_HANDLER( dac_10bit_w )
  1940. {
  1941.     static UINT8 even_byte;
  1942.     struct dac_state *d = &dac[6];
  1943.     int count = (d->bufin - d->bufout) & DAC_BUFFER_SIZE_MASK;
  1944.  
  1945.     /* warning: this assumes all port writes here are word-sized */
  1946.     /* if the offset is even, just stash the value */
  1947.     if (!(offset & 1))
  1948.     {
  1949.         even_byte = data;
  1950.         return;
  1951.     }
  1952.     data = ((data & 0xff) << 8) | even_byte;
  1953.  
  1954.     /* set the new value */
  1955.     d->value = (INT16)data - 0x200;
  1956.     if (LOG_DAC) logerror("%05X:DAC 10-bit value = %02X\n", cpu_get_pc(), data);
  1957.  
  1958.     /* if we haven't overflowed the buffer, add the value value to it */
  1959.     if (count < DAC_BUFFER_SIZE - 1)
  1960.     {
  1961.         /* if this is the first byte, sync the stream */
  1962.         if (count == 0)
  1963.             stream_update(nondma_stream, 0);
  1964.  
  1965.         /* prescale by the volume */
  1966.         d->buffer[d->bufin] = d->value * (0xff / DAC_VOLUME_SCALE / 2);
  1967.         d->bufin = (d->bufin + 1) & DAC_BUFFER_SIZE_MASK;
  1968.  
  1969.         /* update the clock status */
  1970.         if (++count > d->buftarget)
  1971.             clock_active &= ~0x40;
  1972.     }
  1973. }
  1974.  
  1975.  
  1976. static WRITE_HANDLER( ataxx_dac_control )
  1977. {
  1978.     /* handle common offsets */
  1979.     switch (offset)
  1980.     {
  1981.         case 0x00:
  1982.         case 0x02:
  1983.         case 0x04:
  1984.             dac_w(offset, data);
  1985.             return;
  1986.  
  1987.         case 0x06:
  1988.             dac_w(1, ((data << 5) & 0xe0) | ((data << 2) & 0x1c) | (data & 0x03));
  1989.             dac_w(3, ((data << 2) & 0xe0) | ((data >> 1) & 0x1c) | ((data >> 4) & 0x03));
  1990.             dac_w(5, (data & 0xc0) | ((data >> 2) & 0x30) | ((data >> 4) & 0x0c) | ((data >> 6) & 0x03));
  1991.             return;
  1992.     }
  1993.  
  1994.     /* if we have a YM2151 (and an external DAC), handle those offsets */
  1995.     if (has_ym2151)
  1996.     {
  1997.         stream_update(extern_stream, 0);
  1998.         switch (offset)
  1999.         {
  2000.             case 0x08:
  2001.             case 0x09:
  2002.                 ext_active = 1;
  2003.                 if (LOG_EXTERN) logerror("External DAC active\n");
  2004.                 return;
  2005.  
  2006.             case 0x0a:
  2007.             case 0x0b:
  2008.                 ext_active = 0;
  2009.                 if (LOG_EXTERN) logerror("External DAC inactive\n");
  2010.                 return;
  2011.  
  2012.             case 0x0c:
  2013.                 ext_start = (ext_start & 0xff00f) | ((data << 4) & 0x00ff0);
  2014.                 if (LOG_EXTERN) logerror("External DAC start = %05X\n", ext_start);
  2015.                 return;
  2016.  
  2017.             case 0x0d:
  2018.                 ext_start = (ext_start & 0x00fff) | ((data << 12) & 0xff000);
  2019.                 if (LOG_EXTERN) logerror("External DAC start = %05X\n", ext_start);
  2020.                 return;
  2021.  
  2022.             case 0x0e:
  2023.                 ext_stop = (ext_stop & 0xff00f) | ((data << 4) & 0x00ff0);
  2024.                 if (LOG_EXTERN) logerror("External DAC stop = %05X\n", ext_stop);
  2025.                 return;
  2026.  
  2027.             case 0x0f:
  2028.                 ext_stop = (ext_stop & 0x00fff) | ((data << 12) & 0xff000);
  2029.                 if (LOG_EXTERN) logerror("External DAC stop = %05X\n", ext_stop);
  2030.                 return;
  2031.  
  2032.             case 0x42:
  2033.             case 0x43:
  2034.                 dac_w(offset - 0x42 + 14, data);
  2035.                 return;
  2036.         }
  2037.     }
  2038.     logerror("%05X:Unexpected peripheral write %d/%02X = %02X\n", cpu_get_pc(), 5, offset, data);
  2039. }
  2040.  
  2041.  
  2042.  
  2043. /*************************************
  2044.  *
  2045.  *    Peripheral chip dispatcher
  2046.  *
  2047.  *************************************/
  2048.  
  2049. static READ_HANDLER( peripheral_r )
  2050. {
  2051.     int select = offset / 0x80;
  2052.     offset &= 0x7f;
  2053.  
  2054.     switch (select)
  2055.     {
  2056.         case 0:
  2057.             if (offset & 1)
  2058.                 return 0;
  2059.  
  2060.             /* we have to return 0 periodically so that they handle interrupts */
  2061.             if ((++clock_tick & 7) == 0)
  2062.                 return 0;
  2063.  
  2064.             /* if we've filled up all the active channels, we can give this CPU a reset */
  2065.             /* until the next interrupt */
  2066.             {
  2067.                 UINT8 result;
  2068.  
  2069.                 if (!is_redline)
  2070.                     result = ((clock_active >> 1) & 0x3e);
  2071.                 else
  2072.                     result = ((clock_active << 1) & 0x7e);
  2073.  
  2074.                 if (!i186.intr.pending && active_mask && (*active_mask & result) == 0 && ++total_reads > 100)
  2075.                 {
  2076.                     if (LOG_OPTIMIZATION) logerror("Suspended CPU: active_mask = %02X, result = %02X\n", *active_mask, result);
  2077.                     cpu_spinuntil_trigger(CPU_RESUME_TRIGGER);
  2078.                 }
  2079.                 else if (LOG_OPTIMIZATION)
  2080.                 {
  2081.                     if (i186.intr.pending) logerror("(can't suspend - interrupt pending)\n");
  2082.                     else if (active_mask && (*active_mask & result) != 0) logerror("(can't suspend: mask=%02X result=%02X\n", *active_mask, result);
  2083.                 }
  2084.  
  2085.                 return result;
  2086.             }
  2087.  
  2088.         case 1:
  2089.             return main_to_sound_comm_r(offset);
  2090.  
  2091.         case 2:
  2092.             return pit8254_r(offset);
  2093.  
  2094.         case 3:
  2095.             if (!has_ym2151)
  2096.                 return pit8254_r(offset | 0x80);
  2097.             else
  2098.                 return (offset & 1) ? 0 : YM2151_status_port_0_r(offset);
  2099.  
  2100.         case 4:
  2101.             if (is_redline)
  2102.                 return pit8254_r(offset | 0x100);
  2103.             else
  2104.                 logerror("%05X:Unexpected peripheral read %d/%02X\n", cpu_get_pc(), select, offset);
  2105.             break;
  2106.  
  2107.         default:
  2108.             logerror("%05X:Unexpected peripheral read %d/%02X\n", cpu_get_pc(), select, offset);
  2109.             break;
  2110.     }
  2111.     return 0xff;
  2112. }
  2113.  
  2114.  
  2115. static WRITE_HANDLER( peripheral_w )
  2116. {
  2117.     int select = offset / 0x80;
  2118.     offset &= 0x7f;
  2119.  
  2120.     switch (select)
  2121.     {
  2122.         case 1:
  2123.             sound_to_main_comm_w(offset, data);
  2124.             break;
  2125.  
  2126.         case 2:
  2127.             pit8254_w(offset, data);
  2128.             break;
  2129.  
  2130.         case 3:
  2131.             if (!has_ym2151)
  2132.                 pit8254_w(offset | 0x80, data);
  2133.             else if (offset == 0)
  2134.                 YM2151_register_port_0_w(offset, data);
  2135.             else if (offset == 2)
  2136.                 YM2151_data_port_0_w(offset, data);
  2137.             break;
  2138.  
  2139.         case 4:
  2140.             if (is_redline)
  2141.                 pit8254_w(offset | 0x100, data);
  2142.             else
  2143.                 dac_10bit_w(offset, data);
  2144.             break;
  2145.  
  2146.         case 5:    /* Ataxx/WSF/Indy Heat only */
  2147.             ataxx_dac_control(offset, data);
  2148.             break;
  2149.  
  2150.         default:
  2151.             logerror("%05X:Unexpected peripheral write %d/%02X = %02X\n", cpu_get_pc(), select, offset, data);
  2152.             break;
  2153.     }
  2154. }
  2155.  
  2156.  
  2157.  
  2158. /*************************************
  2159.  *
  2160.  *    Optimizations
  2161.  *
  2162.  *************************************/
  2163.  
  2164. void leland_i86_optimize_address(offs_t offset)
  2165. {
  2166.     if (offset)
  2167.         active_mask = memory_region(REGION_CPU3) + offset;
  2168.     else
  2169.         active_mask = NULL;
  2170. }
  2171.  
  2172.  
  2173.  
  2174. /*************************************
  2175.  *
  2176.  *    Game-specific handlers
  2177.  *
  2178.  *************************************/
  2179.  
  2180. WRITE_HANDLER( ataxx_i86_control_w )
  2181. {
  2182.     /* compute the bit-shuffled variants of the bits and then write them */
  2183.     int modified =     ((data & 0x01) << 7) |
  2184.                     ((data & 0x02) << 5) |
  2185.                     ((data & 0x04) << 3) |
  2186.                     ((data & 0x08) << 1);
  2187.     leland_i86_control_w(offset, modified);
  2188. }
  2189.  
  2190.  
  2191.  
  2192. /*************************************
  2193.  *
  2194.  *    Sound CPU memory handlers
  2195.  *
  2196.  *************************************/
  2197.  
  2198. struct MemoryReadAddress leland_i86_readmem[] =
  2199. {
  2200.     { 0x00000, 0x03fff, MRA_RAM },
  2201.     { 0x0c000, 0x0ffff, MRA_BANK6 },    /* used by Ataxx */
  2202.     { 0x1c000, 0x1ffff, MRA_BANK7 },    /* used by Super Offroad */
  2203.     { 0x20000, 0xfffff, MRA_ROM },
  2204.     { -1 }  /* end of table */
  2205. };
  2206.  
  2207. struct MemoryWriteAddress leland_i86_writemem[] =
  2208. {
  2209.     { 0x00000, 0x03fff, MWA_RAM, &ram_base },
  2210.     { 0x0c000, 0x0ffff, MWA_BANK6 },
  2211.     { 0x1c000, 0x1ffff, MWA_BANK7 },
  2212.     { 0x20000, 0xfffff, MWA_ROM },
  2213.     { -1 }  /* end of table */
  2214. };
  2215.  
  2216. struct IOReadPort leland_i86_readport[] =
  2217. {
  2218.     { 0xff00, 0xffff, i186_internal_port_r },
  2219.     { -1 }  /* end of table */
  2220. };
  2221.  
  2222. struct IOWritePort redline_i86_writeport[] =
  2223. {
  2224.     { 0x6000, 0x6fff, redline_dac_w },
  2225.     { 0xff00, 0xffff, i186_internal_port_w },
  2226.     { -1 }  /* end of table */
  2227. };
  2228.  
  2229. struct IOWritePort leland_i86_writeport[] =
  2230. {
  2231.     { 0x0000, 0x000b, dac_w },
  2232.     { 0x0080, 0x008b, dac_w },
  2233.     { 0x00c0, 0x00cb, dac_w },
  2234.     { 0xff00, 0xffff, i186_internal_port_w },
  2235.     { -1 }  /* end of table */
  2236. };
  2237.  
  2238. struct IOWritePort ataxx_i86_writeport[] =
  2239. {
  2240.     { 0xff00, 0xffff, i186_internal_port_w },
  2241.     { -1 }  /* end of table */
  2242. };
  2243.  
  2244.  
  2245. /************************************************************************
  2246.  
  2247. Memory configurations:
  2248.  
  2249.     Redline Racer:
  2250.         FFDF7:80186 upper chip select = E03C        -> E0000-FFFFF, 128k long
  2251.         FFDF7:80186 lower chip select = 00FC        -> 00000-00FFF, 4k long
  2252.         FFDF7:80186 peripheral chip select = 013C    -> 01000, 01080, 01100, 01180, 01200, 01280, 01300
  2253.         FFDF7:80186 middle chip select = 81FC        -> 80000-C0000, 64k chunks, 256k total
  2254.         FFDF7:80186 middle P chip select = A0FC
  2255.  
  2256.     Quarterback, Team Quarterback, AAFB, Super Offroad, Track Pack, Pigout, Viper:
  2257.         FFDFA:80186 upper chip select = E03C        -> E0000-FFFFF, 128k long
  2258.         FFDFA:80186 peripheral chip select = 203C    -> 20000, 20080, 20100, 20180, 20200, 20280, 20300
  2259.         FFDFA:80186 middle chip select = 01FC        -> 00000-7FFFF, 128k chunks, 512k total
  2260.         FFDFA:80186 middle P chip select = C0FC
  2261.  
  2262.     Ataxx, Indy Heat, World Soccer Finals:
  2263.         FFD9D:80186 upper chip select = E03C        -> E0000-FFFFF, 128k long
  2264.         FFD9D:80186 peripheral chip select = 043C    -> 04000, 04080, 04100, 04180, 04200, 04280, 04300
  2265.         FFD9D:80186 middle chip select = 01FC        -> 00000-7FFFF, 128k chunks, 512k total
  2266.         FFD9D:80186 middle P chip select = C0BC
  2267.  
  2268. ************************************************************************/
  2269.